package com.example.mybatisdemo1.service;

import com.example.mybatisdemo1.entity.ActualAirportBaseinfo;
import com.example.mybatisdemo1.entity.TestBatchInsertThread;
import com.example.mybatisdemo1.util.ComUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: xiaoqiang
 * @Description:
 * @Date: 2020/9/4
 * @Version: 1.0
 */
@Service
public class AirPortService {

    public void doMethod(AirPortService AirPortService) {
        try {
            String path = "/Users/xiaoqiang/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/731711a7be99e6ee8f7630c464716f71/Message/MessageTemp/10f624701b4872dcd5c449b7622d49f7/File/";
            InputStream is = new FileInputStream(new File(path +
                    "外包人员统计表(1).xlsx"));
            Workbook wb = new XSSFWorkbook(is);
            Sheet sheet = wb.getSheetAt(0);
            //excel数据总量
            int rowSize = sheet.getLastRowNum() + 1;
            //每个线程处理的数量
            int count = 10;
            //开启的线程数
            int runSize = (rowSize / count) + 1;
            long time1 = System.currentTimeMillis();
            // 创建一个线程池，数量和开启线程的数量一样
            ExecutorService executor = Executors.newFixedThreadPool(runSize);
            // 创建两个个计数器
            CountDownLatch begin = new CountDownLatch(1);
            CountDownLatch end = new CountDownLatch(runSize);

            //为每个线程分配要执行的数据
            for (int i = 0; i < runSize; i++) {
                int startIdx = 0;
                int endIdx = 0;
                if ((i + 1) == runSize) {
                    startIdx = (i * count);
                    endIdx = rowSize;
                } else {
                    startIdx = (i * count);
                    endIdx = (i + 1) * count;
                }
                TestBatchInsertThread thread = new TestBatchInsertThread(sheet, begin, end, AirPortService, startIdx, endIdx);
                executor.execute(thread);
            }
            begin.countDown();
            end.await();

            executor.shutdown();
            long time2 = System.currentTimeMillis();
            System.out.println("总耗时：" + (time2 - time1));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理excel,插入数据库
     *
     * @param sheet
     * @param sta
     * @param end
     * @return
     */
    public String ExcelDoing(Sheet sheet, int sta, int end) {
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            ActualAirportBaseinfo ab = null;
            List<ActualAirportBaseinfo> list = new
                    ArrayList<ActualAirportBaseinfo>();
            for (int y = sta; y < end; y++) {
                ab = new ActualAirportBaseinfo();
                Row row = sheet.getRow(y);
                String xuyao = ComUtil.getCellValue(row.getCell(0));
                String name = ComUtil.getCellValue(row.getCell(1));
                String jigou = ComUtil.getCellValue(row.getCell(2));
                String bumen = ComUtil.getCellValue(row.getCell(3));
                String idCard = ComUtil.getCellValue(row.getCell(4));
                String bir = ComUtil.getCellValue(row.getCell(6));


                ab.setXuhao(xuyao);
                ab.setName(name);
                ab.setJigou(jigou);
                ab.setBumen(bumen);
                ab.setIdCard(idCard);
                if(!"".equals(bir) && bir.contains("-")){
                    ab.setBirthday(sf.parse(bir));
                }



                list.add(ab);
            }
            System.out.println("list插入数据----------》" + list);
            //插入数据库
//            actualAirportBaseinfoMapper.insertAll(list);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
